package com.zzq.demo.tio.server.handler;

import com.zzq.demo.tio.common.HelloPacket;
import com.zzq.demo.tio.common.Type;
import com.zzq.demo.tio.common.bs.AbsBsHandler;
import com.zzq.demo.tio.common.packet.HelloMsgReqBody;
import com.zzq.demo.tio.common.packet.HelloMsgRespBody;
import lombok.extern.slf4j.Slf4j;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.json.Json;

/**
 * @author : zhaoziqian
 * @description :
 * @date : 2019/10/24 10:46
 */
@Slf4j
public class SimpleMsgReqHandler extends AbsBsHandler<HelloMsgReqBody> {

    @Override
    public Class<HelloMsgReqBody> bodyClass() {
        return HelloMsgReqBody.class;
    }

    @Override
    public Object handler(HelloPacket packet, HelloMsgReqBody bsBody, ChannelContext channelContext) throws Exception {
        log.info("收到 simple msg");
        HelloMsgRespBody helloMsgRespBody = new HelloMsgRespBody();
        helloMsgRespBody.setText("你好欢迎联系！！！！！");
        Tio.bindBsId(channelContext, "1234");
        HelloPacket respPacket = new HelloPacket();
        respPacket.setType(Type.SIMPLE_RESP);
        respPacket.setBody(Json.toJson(helloMsgRespBody).getBytes(HelloPacket.CHARSET));
        Tio.send(channelContext, respPacket);
        return null;
    }
}
